package com.dq.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.commonutils.pojo.ResultCode;
import com.dq.commonutils.vo.Result;
import com.dq.eduservice.entity.EduTeacher;
import com.dq.eduservice.service.EduTeacherService;
import com.dq.eduservice.vo.param.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * $tableInfo.name-API
 *
 * @author Dq
 * @since 2022-07-14 19:53:03
 */
@RestController
@RequestMapping("eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("findAll")
    public Result findAllTeacher() {
        return Result.success(eduTeacherService.list(null));
    }


    /**
     * 分页查询
     * url : /eduservice/teacher/pageTeacher/{current}/{pageSize}
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("pageTeacher/{current}/{pageSize}")
    public Result findTeacherByPage(@PathVariable(value = "current") Long current,
                                    @PathVariable("pageSize") Long pageSize) {
        Page<EduTeacher> page = new Page<>(current, pageSize);
        return Result.success(eduTeacherService.page(page, null));

    }

    /**
     * 根据条件分页查询
     * @param pageParam
     * @return
     */
    @PostMapping("findTeacherByCondition")
    public Result findTeacherByPage(@RequestBody(required = false) PageParam pageParam) {
        Page<EduTeacher> page = new Page<>(pageParam.getPage(), pageParam.getPageSize());
        //条件构造器 动态拼接SQL
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        //姓名不为空
        if (!StringUtils.isEmpty(pageParam.getName())){
            queryWrapper.like(EduTeacher::getName,pageParam.getName());
        }
        if (!StringUtils.isEmpty(pageParam.getLevel())){
            queryWrapper.eq(EduTeacher::getLevel,pageParam.getLevel());
        }
        if (!StringUtils.isEmpty(pageParam.getBegin())){
            queryWrapper.ge(EduTeacher::getGmtCreate,pageParam.getBegin());
        }
        if (!StringUtils.isEmpty(pageParam.getEnd())){
            queryWrapper.le(EduTeacher::getGmtModified,pageParam.getEnd());
        }
        //降序排列
        queryWrapper.orderByDesc(EduTeacher::getGmtCreate);
        Page<EduTeacher> teacherPage = eduTeacherService.page(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",teacherPage.getTotal());
        map.put("rows",teacherPage.getRecords());
        map.put("current",teacherPage.getCurrent());
        map.put("pageSize",teacherPage.getSize());
        return Result.success(map);

    }

    /**
     * 通过id查看教室
     * @param id
     * @return
     */
    @PostMapping("findTeacher/{id}")
    public Result findTeacherById(@PathVariable("id") String id) {
     return Result.success(eduTeacherService.getById(id));
    }


    /**
     * 根据id 逻辑删除
     * @param id
     * @return
     */
    @PostMapping("{id}")
    public Result findAllTeacher(@PathVariable("id") String id) {

        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return Result.success(flag);
        } else {
            return Result.error(ResultCode.ERROR.getCode(), ResultCode.ERROR.getMsg());
        }
    }

    /**
     * 添加用户
     * @param eduTeacher
     * @return
     */
    @PostMapping("addEduTeacher")
    public Result addEduTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);

        if (flag){
            return Result.success(null);
        }else {
            return Result.error(ResultCode.SESSION_TIME_OUT.getCode(), ResultCode.SESSION_TIME_OUT.getMsg());
        }
    }


    /**
     * 修改老师
     * @param eduTeacher
     * @return
     */
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return Result.success(null);
        }else {
            return Result.error(ResultCode.SESSION_TIME_OUT.getCode(), ResultCode.SESSION_TIME_OUT.getMsg());
        }
    }


    /**
     * 统一返回数据测试
     * @return
     */
    @GetMapping("test")
    public Result updateTeacher(){
        //测试这个bug  by zero
        int i= 10/0;
        return Result.success("成功返回");
    }
}